package com.algernoon.nettydemo.bio.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TimeServer {

	// 同步阻塞I/O服务
	public static void main(String[] args) throws IOException {
		
		int port = 8080;
		if (args != null && args.length > 0) {
			try {
				port = Integer.valueOf(args[0]);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		ServerSocket server = null;
		try {
			server = new ServerSocket(port);
			System.out.println("The time server is started in port: " + port);
			Socket socket = null;
			while(true) {
				socket = server.accept();
				// 线程执行完毕要释放资源，供下个客户端请求使用
				new Thread(new TimeServerHandler(socket)).start();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (server != null) {
				System.out.println("close time server!");
				server.close();
				server = null;
			}
		}
		
	}
	
}
